package com.glory.dgcpcpda.activity.technicalservice;


import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.FixtureCleaningQueryBean;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;

public class FixtureCleaningActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.warehouse_id)
    TextView warehouse_id;
    @BindView(R.id.toolspec)
    TextView toolspec;
    @BindView(R.id.toolid)
    TextView toolid;
    @BindView(R.id.grade2)
    TextView grade2;
    @BindView(R.id.eapparam)
    TextView eapparam;
    @BindView(R.id.clean_state)
    TextView clean_state;
    @BindView(R.id.clean_date)
    TextView clean_date;
    @BindView(R.id.state)
    TextView state;
    String materialtype;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fixture_cleaning;
    }

    @Override
    protected void initView() {

        tvTitle.setText("治具清洗");
        tvBaseSubmit.setText("清洗");

    }

    @Override
    protected void initData() {

        initEdit(etEqpWork, s -> {
            query();
        });

        tvBaseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (etEqpWork.getText().toString().isEmpty()){
                    showToast("请扫描治具编号",false);
                    return;
                }
                submit();
            }
        });
    }

    private void query() {

        JsonObject body = new JsonObject();
        body.addProperty("ACTIONTYPE", "QUERYBYID");
        body.addProperty("MATERIALTYPE", "Tooling");
        body.addProperty("MLOTID", etEqpWork.getText().toString());
        NetUtils.requestPostNet("CPC.INVENTORYFINDMLOTBYID", "TRANSFERMES", this, body, new FreeUI_DialogEntityCallBack<BaseResponseBean<FixtureCleaningQueryBean>>
                (new TypeToken<BaseResponseBean<FixtureCleaningQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<FixtureCleaningQueryBean>> response) {

                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    FixtureCleaningQueryBean.XToolDTO xTool = response.body().getResponse().getBody().getXTool();
                    warehouse_id.setText(xTool.getWAREHOUSE_ID());
                    toolspec.setText(xTool.getTOOLSPEC());
                    toolid.setText(xTool.getTOOLID());
                    grade2.setText(xTool.getGRADE2());
                    eapparam.setText(xTool.getEAPPARAM());
                    clean_state.setText(xTool.getCLEAN_STATE());
                    clean_date.setText(xTool.getCLEAN_DATE());
                    state.setText(xTool.getSTATE());
                    materialtype = xTool.getMATERIAL_TYPE();

                }

            }

            @Override
            public void onError(Response<BaseResponseBean<FixtureCleaningQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "TRANSFERMES");
            }
        });

    }


    private void submit() {

        JsonObject body = new JsonObject();
        body.addProperty("ACTIONTYPE", "SAVE");
        body.addProperty("MATERIALTYPE", materialtype);
        body.addProperty("CLEAN_STATE", "Clean");
        body.addProperty("STATE", state.getText().toString());
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        body.addProperty("CLEAN_DATE", dateFormat.format(currentDate));
        body.addProperty("MLOTID", etEqpWork.getText().toString());
        NetUtils.requestPostNet("CPC.INVENTORYSAVEMLOTBYID", "TRANSFERMES", this, body, new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                (new TypeToken<BaseResponseBean<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {

                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {

                    showToast("清洗成功", true);
                    warehouse_id.setText("");
                    toolspec.setText("");
                    toolid.setText("");
                    grade2.setText("");
                    eapparam.setText("");
                    clean_state.setText("");
                    clean_date.setText("");
                    state.setText("");
                    etEqpWork.setText("");
                    etEqpWork.requestFocus();
//                    new Handler().postDelayed(() -> {
//                        finish();
//                    }, 2000);

                }

            }

            @Override
            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                super.onError(response);
                loadError(response.getException(), "TRANSFERMES");
            }
        });
    }




    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(Constant.CURRENT_USER);
        return new FreeUI_AddViewEntity("header", llMain);
    }
}